package com.example.myapplication.Algorithm.CenterSubscript;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.Arrays;

public class CenterSubscript {
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void main(String[] args){
        System.out.println(pivotIndex(new int[]{1,7,3  ,6,  5,6}));
//        System.out.println(pivotIndex(new int[]{9,8,7,6,5,4,3,1,3,4,5,6,7,8,9}));
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static int pivotIndex(int[] nums){
        int sum = Arrays.stream(nums).sum();///流式计算数组的和 28
        System.out.println("sum:"+sum);
        int total = 0;//总计
        for (int i=0;i<nums.length;i++){
            total = total +nums[i];
            if(total == sum){
                System.out.println("total:"+total+"sum:"+sum);
                return i;
            }
            //27 = 28 -  1
            //sum - nums[i]代表右边的总和
            sum = sum - nums[i];
        }
        return  -1;
    }
}
